package com.hyperion.blog.controller;

import com.hyperion.blog.common.aop.LogAnnotation;
import com.hyperion.blog.service.ArticleService;
import com.hyperion.blog.vo.ErrorCode;
import com.hyperion.blog.vo.params.ArticleParam;
import com.hyperion.blog.vo.params.PageParams;
import com.hyperion.blog.vo.result.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author Hyperion
 */
@RestController
@RequestMapping("articles")
public class ArticleController {

    @Resource
    private ArticleService articleService;

    /**
     * 首页 文章列表
     *
     * @param pageParams 分页参数
     * @return 返回查询分页
     */
    @PostMapping
    @LogAnnotation(clazz = "ArticleController", method = "listArticle")
    public Result listArticle(@RequestBody PageParams pageParams) {
        return Result.success(articleService.listArticle(pageParams));
    }

    @RequestMapping("/new")
    @LogAnnotation(clazz = "ArticleController", method = "newArticle")
    public Result newArticle() {
        int limit = 6;
        return Result.success(articleService.newArticle(limit));
    }

    @RequestMapping("/hot")
    @LogAnnotation(clazz = "ArticleController", method = "hotArticle")
    public Result hotArticle() {
        int limit = 6;
        return Result.success(articleService.hotArticle(limit));
    }

    @PostMapping("/listArchives")
    @LogAnnotation(clazz = "ArticleController", method = "listArchives")
    public Result listArchives() {
        return Result.success(articleService.listArchives());
    }

    @PostMapping("/view/{id}")
    @LogAnnotation(clazz = "ArticleController", method = "findArticleById")
    public Result findArticleById(@PathVariable("id") Long articleId) {
        Object data = articleService.findArticleById(articleId);
        if (data == null) {
            return Result.fail(ErrorCode.PARAMS_ERROR);
        } else {
            return Result.success(data);
        }
    }

    @PostMapping("/publish")
    @LogAnnotation(clazz = "ArticleController", method = "articlePublish")
    public Result articlePublish(@RequestBody ArticleParam articleParam) {
        return Result.success(articleService.publish(articleParam));
    }

}